﻿namespace TfsExtensions.Build.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using System.Collections.ObjectModel;
    using Microsoft.TeamFoundation.Framework.Common;
    using Microsoft.TeamFoundation;
    using System.Net;
    using System.Diagnostics;
    using Microsoft.TeamFoundation.Build.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
    using System.IO;
    using System.Reflection;
    using Microsoft.TeamFoundation.Server;
    using TfsExtensions.Build.Controls;

    class IOHelper
    {
        public static void RemoveReadOnlyAttribute(string fileName)
        {
            if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
            }
        }

        public static void DeleteReadOnlyDirectory(string dirPath)
        {
            if (!string.IsNullOrEmpty(dirPath) && Directory.Exists(dirPath))
            {
                RemoveDirectoryReadOnlyAttribute(dirPath);
                Directory.Delete(dirPath, true);
            }
        }

        public static void RemoveDirectoryReadOnlyAttribute(string dirPath)
        {
            if (!string.IsNullOrEmpty(dirPath) && Directory.Exists(dirPath))
            {
                string[] files = Directory.GetFiles(dirPath);
                foreach (string file in files)
                {
                    RemoveReadOnlyAttribute(file);
                }

                string[] directories = Directory.GetDirectories(dirPath);
                foreach (string directory in directories)
                {
                    RemoveDirectoryReadOnlyAttribute(directory);
                }
            }
        }
    }
}
